package com.luyun.frogpay.base.baseclass;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.NetworkUtils;
import com.luyun.frogpay.base.application.BaseApplication;
import com.luyun.frogpay.base.dagger.activity.component.ActivityComponent;
import com.luyun.frogpay.base.dagger.activity.component.DaggerActivityComponent;
import com.luyun.frogpay.base.dagger.activity.module.ActivityModule;
import com.luyun.frogpay.base.utils.Log;
import com.trello.rxlifecycle3.LifecycleTransformer;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author 无人区
 * @date 2019/10/27 20:24
 * @desc:
 */
public abstract class BaseActivity<T extends BaseContract.BasePresenter> extends RxAppCompatActivity implements BaseContract.BaseView {

    @Inject
    protected T presenter;

    protected Context context = BaseApplication.context;

    protected ActivityComponent activityComponent;

    private Unbinder unbinder;
    protected LoadingDialog loadingDialog;


    protected abstract void initView();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActivityComponent();
        //接收 ARouter 传参
        ARouter.getInstance().inject(context);
        //无标题显示
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        int layoutId = getLayoutId();
        setContentView(layoutId);
        initInjector();
        unbinder = ButterKnife.bind(this);
        attachView();
        initView();
        initLoading();
        if (!NetworkUtils.isConnected()){
            //无网络
        }
        Log.d("model:  " + Build.MODEL);
    }

    /**
     * 初始化 ActivityComponent
     */
    private void initActivityComponent(){
        activityComponent = DaggerActivityComponent.builder()
                .applicationComponent(((BaseApplication)getApplication()).getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .build();
    }

    protected abstract void initInjector();

    protected abstract int getLayoutId();

    /**
     * 引入 presenter
     */
    private void attachView() {
        if (null != presenter){
            presenter.attachView(this);
        }
    }

    /**
     * 分离 presenter
     */
    private void detachView(){
        if (null != presenter){
            presenter.detachView();
        }
    }

    /**
     * 显示 loading
     */
    @Override
    public void isShowLoading(boolean isShow) {
        if (isShow){
            loadingDialog.show();
        }else {
            loadingDialog.close();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        detachView();
    }

    /**
     * 当前应用 sha1 值
     * @param context
     * @return
     */
    public String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1) {
                    hexString.append("0");
                }
                hexString.append(appendString);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length() - 1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void initLoading(){
        loadingDialog = new LoadingDialog(this)
                .setInterceptBack(false);
    }


    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return bindToLifecycle();
    }
}
